<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="登录地址" prop="createBy">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入登录地址"
            @keyup.enter="handleQuery"
            class="input-inner"
          />
        </el-form-item>

        <el-form-item label="用户名称" prop="createBy">
          <el-input
            v-model="queryParams.loginName"
            placeholder="请输入用户名称"
            @keyup.enter="handleQuery"
            class="input-inner"
          />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="全部"
            clearable
            class="!w-[100px]"
          >
            <el-option :value="0" label="成功"/>
            <el-option :value="1" label="失败"/>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button class="filter-item" type="primary" @click="handleQuery">
            <i-ep-search/>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh/>
            重置
          </el-button>

          <el-button class="ml-3" @click="handleExport">
            <template #icon>
              <i-ep-download/>
            </template>
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">

      <el-table
        v-loading="loading"
        :data="logList"
        row-key="id"
        height="calc(100vh - 290px)"
      >
        <el-table-column prop="infoId" label="访问ID" :min-width="90"/>
        <el-table-column prop="loginName" label="登录账号"  :min-width="100">
        </el-table-column>
        <el-table-column prop="ipaddr" label="登录地址" align="center" :min-width="300"/>
        <el-table-column prop="loginLocation" label="登录地点" :min-width="100"/>
        <el-table-column prop="browser" label="浏览器" :min-width="100"/>
        <el-table-column prop="os" label="操作系统" :min-width="100"/>
        <el-table-column label="登录状态" :min-width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === '0'" type="success">成功</el-tag>
            <el-tag v-else type="danger">失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="msg" label="操作消息" :min-width="100"/>
        <el-table-column prop="loginTime" label="登录日期" align="center" :min-width="200"/>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

  </div>
</template>

<script setup lang="ts">
import {exportLog, listLogs} from "@/api/log/loginInfo";

import {LoginInfoLogQuery, LoginInfoLogVO} from "@/api/log/loginInfo/types";

defineOptions({
  name: "LoginLog",
  inheritAttrs: false
});

const queryFormRef = ref(ElForm);
const loading = ref(false);

const total = ref(0)
const dateRange = ref('')

const queryParams = reactive<LoginInfoLogQuery>({
  pageNum: 1,
  pageSize: 10,
});

const logList = ref<LoginInfoLogVO[]>();

/** 查询 */
function handleQuery() {
  loading.value = true;
  listLogs(queryParams)
    .then(({data}) => {
      logList.value = data.list;
      total.value = parseInt(data.total);
    })
    .finally(() => {
      loading.value = false;
    });
}

/**重置查询 */
function resetQuery() {
  queryParams.pageNum = 1;
  dateRange.value = "";
  queryParams.beginTime = ''
  queryParams.endTime = ''
  queryFormRef.value.resetFields();
  handleQuery();
}


function handleExport() {
  console.log('导出')
  exportLog(queryParams).then((response: any) => {
    const fileData = response.data;
    const fileName = decodeURI(
      response.headers["content-disposition"].split(";")[1].split("=")[1]
    );
    const fileType =
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";

    const blob = new Blob([fileData], {type: fileType});
    const downloadUrl = window.URL.createObjectURL(blob);

    const downloadLink = document.createElement("a");
    downloadLink.href = downloadUrl;
    downloadLink.download = fileName;

    document.body.appendChild(downloadLink);
    downloadLink.click();

    document.body.removeChild(downloadLink);
    window.URL.revokeObjectURL(downloadUrl);
  });
}

onMounted(() => {
  handleQuery();
});

watch(dateRange, (newVal) => {
  console.log(newVal)
  if (newVal) {
    queryParams.beginTime = newVal[0] + " 00:00:00";
    queryParams.endTime = newVal[1] + " 23:59:59";
  }
});
</script>
